package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"user/model/mysql"

	"user/userrpc/internal/svc"
	"user/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *userrpc.UserUpdateRequest) (*userrpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var find mysql.User
	mysql.DB.Debug().Where("id = ?", in.Userid).Find(&find)
	if find.ID == 0 {
		return nil, fmt.Errorf("用户登录信息异常")
	}
	user := mysql.User{
		Model:     gorm.Model{ID: uint(in.Userid)},
		HeadImage: in.HeadImage,
		Phone:     in.Phone,
		Sex:       in.Sex,
	}
	err := mysql.DB.Debug().Updates(&user).Error
	if err != nil {
		return nil, fmt.Errorf("用户信息修改失败")
	}
	return &userrpc.UserUpdateResponse{Id: int64(user.ID)}, nil
}
